package com.woniuxy.warehouse.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.entity.warehouse.IoWarehouse;
import com.woniuxy.commons.entity.warehouse.dto.StockInHomePageDto;
import com.woniuxy.commons.entity.warehouse.dto.TimeAndOrderDto;
import com.woniuxy.commons.result.Result;
import com.woniuxy.warehouse.mapper.StockInHomePageDao;
import com.woniuxy.warehouse.mapper.StockOutMapper;
import com.woniuxy.warehouse.service.StockOutService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:SHEAndme
 * @Todo:TODO 出库实现类
 * @DateTime:2022/4/19 10:05
 * @Component:霍格沃兹魔法学院
 **/
@Service
public class StockOutServiceImpl implements StockOutService {
    @Resource
    private StockOutMapper stockOutMapper;
    @Resource
    private StockInHomePageDao stockInHomePageDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Result<List<IoWarehouse>> queryById(Integer id) {
//        return Result.ok();
        List<IoWarehouse> list = stockOutMapper.queryById(id);
        return Result.ok(list);


    }
    /**
     * @param currentPage:
     * @param pageSize:
     * @param ioWarehouse:
     * @return Result
     * @description TODO 分页查询
     */
    @Override
    public Result queryByPage(Integer currentPage, Integer pageSize, IoWarehouse ioWarehouse) {
        PageHelper.startPage(currentPage,pageSize);
        List<IoWarehouse> ioWarehousesList = stockOutMapper.findAll(ioWarehouse);

        PageInfo<IoWarehouse> of = PageInfo.of(ioWarehousesList);

        Result<PageInfo> listResult = new Result<>();
        if(ioWarehousesList.size()>0){
            listResult.setData(of);
            listResult.setCode(200);
            listResult.setMessage("查询成功");
        }else {
            listResult.setData(null);
            listResult.setCode(500);
            listResult.setMessage("数据为空");
        }
        return listResult;
    }

    /**
     * 修改数据
     *
     * @param ioWarehouse 实例对象
     * @return 实例对象
     */
    @Override
    public Result update(IoWarehouse ioWarehouse) {
        stockOutMapper.update(ioWarehouse);
        return this.queryById(ioWarehouse.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return stockOutMapper.deleteById(id) > 0;
    }

    @Override
    public Result findAllStockOutHomePage(Integer currentPage, Integer pageSize, TimeAndOrderDto timeAndOrderDto) {
                PageHelper.startPage(currentPage,pageSize);

                List<StockInHomePageDto> stockOutHomeList = stockInHomePageDao.StockOutFindAllByTimeAndOrderNo(timeAndOrderDto);

                PageInfo<StockInHomePageDto> of = PageInfo.of(stockOutHomeList);

                Result<PageInfo> listResult  = new Result<>();
                if (stockOutHomeList.size()>0){
                    listResult.setData(of);
                    listResult.setCode(200);
                    listResult.setMessage("查询成功");
                }else {
                    listResult.setData(null);
                    listResult.setCode(500);
                    listResult.setMessage("数据为空");
                }
        return listResult;

    }
/**
 * @param ioWarehouse:
 * @return Result
 * @description TODO 增加新的出库单
 */
    @Override
    public Result addStockOutOrder(IoWarehouse ioWarehouse) {
        stockOutMapper.addStockOutOrder(ioWarehouse);
        return Result.ok();
    }
}
